package com.yiguantong.police.retrofit;


import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.io.IOException;

import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Qiuxiaolong on 2017/04/20.
 */
public class RetrofitUtil {
    public static Retrofit restRetrofit;
    private static final int defaultConnectTimeout = 5;//默认链接超时时间
    private static final int defaultRWTimeout = 25;//默认读写数据超时时间

    private static final String baseUrl = "http://223.99.141.143:80/";

    private static final String baseUrlTest = "http://47.105.154.119:8080/";


    /**
     * restful的APIService实例
     *
     * @return
     */
    public static APIService newApi() {
        if (restRetrofit == null) {
            restRetrofit = new Retrofit
                    .Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(getHttpClient())
                    .baseUrl(baseUrlTest)
                    .build();
        }
        APIService service = restRetrofit.create(APIService.class);
        return service;
    }

    /**
     * 此方法调用外部链接时使用
     */
    public static APIService Api(String baseUrl) {
        Retrofit retrofit = new Retrofit
                .Builder()
                .client(httpNormalClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(baseUrl)
                .build();

        APIService service = retrofit.create(APIService.class);
        return service;
    }

    static OkHttpClient httpClient = null;

    static OkHttpClient getHttpClient() {
        if (httpClient == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(defaultConnectTimeout, TimeUnit.SECONDS)
                    .readTimeout(defaultRWTimeout, TimeUnit.SECONDS)
                    .writeTimeout(defaultRWTimeout, TimeUnit.SECONDS)
                    .addInterceptor(new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = chain.request();
                            Request.Builder builder = request.newBuilder();
                            Request requestBuild = builder.build();
                            Response response = chain.proceed(requestBuild);
                            return response;
                        }
                    });
            httpClient = builder.build();
        }
        return httpClient;
    }
    /**
     * 此方法调用外部链接时使用
     */
    static OkHttpClient httpNormalClient = new OkHttpClient.Builder().
            connectTimeout(defaultConnectTimeout, TimeUnit.SECONDS)
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request();
                    Request.Builder builder = request.newBuilder();
//                    builder.removeHeader("User-Agent").addHeader("User-Agent", new WebView(MainApplication.getIns()).getSettings().getUserAgentString());
                    Request requestBuild = builder.build();
                    Response response = chain.proceed(requestBuild);
                    return response;
                }
            })
            .build();

}